package com.shengdou.as.slopsapptest.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseActivity;
import com.shengdou.as.slopsapptest.bean.ContactUserBean;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.HttpUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.NetConstant;

import java.util.ArrayList;

/**
 * 联系我们的信息
 */
public class ContactUsActivity extends BaseActivity {

    private ListView mListView;
    private LinearLayout gone;
    private ArrayList<ContactUserBean.DataBean> array = new ArrayList<>();
    ;
    private MyAdapter adapter;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constants.CONTACTUS:
                    String results = msg.obj.toString();
                    LogUtil.e("联系我们", results);
                    ContactUserBean contactUser = new Gson().fromJson(results, ContactUserBean.class);
                    if (contactUser.getData() != null && contactUser.getData().size() > 0) {
                        array.clear();
                        array.addAll(contactUser.getData());
                        if (array != null && array.size() > 0) {
                            mListView.setVisibility(View.VISIBLE);
                            gone.setVisibility(View.GONE);
                            mListView.setAdapter(adapter);
                            adapter.setmArray(array);
                        } else {
                            adapter.notifyDataSetChanged();
                            mListView.setVisibility(View.GONE);
                            gone.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_us);
        init();
    }

    private void init() {
        mListView = (ListView) findViewById(R.id.listview);
        gone = (LinearLayout) findViewById(R.id.txt_gone);
        adapter = new MyAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpUtils.post(mHandler, NetConstant.CONTACTUS, Constants.CONTACTUS, ContactUsActivity.this);
    }

    public void onTabClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
           /* case R.id.txt_contact:
                // 电话信息
                final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                alertDialog.show();
                alertDialog.getWindow().setContentView(R.layout.dialog_phone);
                alertDialog.getWindow().findViewById(R.id.dialog_verify_cancle).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        alertDialog.cancel();
                    }
                });
                alertDialog.getWindow().findViewById(R.id.dialog_verify_go).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "13720755705"));
                        startActivity(intent1);
                    }
                });
                break;*/
        }
    }

    class MyAdapter extends BaseAdapter {
        private ArrayList<ContactUserBean.DataBean> mArray;

        public MyAdapter() {
        }

        public void setmArray(ArrayList<ContactUserBean.DataBean> mArray) {
            this.mArray = mArray;
        }

        @Override
        public int getCount() {
            return mArray != null ? mArray.size() : 0;
        }

        @Override
        public ContactUserBean.DataBean getItem(int position) {
            return mArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(ContactUsActivity.this, R.layout.item_contact_user, null);
                holder.mTxtTitle = (TextView) convertView.findViewById(R.id.txt_title);
                holder.mTxtValue = (TextView) convertView.findViewById(R.id.txt_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTxtTitle.setText(mArray.get(position).getTitle());
            holder.mTxtValue.setText(mArray.get(position).getValue());
            final int index = position;
            if (mArray.get(index).getType() == 1) {
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 电话信息
                        final AlertDialog alertDialog = new AlertDialog.Builder(ContactUsActivity.this).create();
                        alertDialog.show();
                        alertDialog.getWindow().setContentView(R.layout.dialog_phone);
                        TextView mTxtPhone = (TextView) alertDialog.getWindow().findViewById(R.id.TXT_TEL);
                        mTxtPhone.setText(mArray.get(index).getValue());
                        alertDialog.getWindow().findViewById(R.id.dialog_verify_cancle).setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                alertDialog.cancel();
                            }
                        });
                        alertDialog.getWindow().findViewById(R.id.dialog_verify_go).setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                alertDialog.cancel();
                                Intent intent1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mArray.get(index).getValue()));
                                startActivity(intent1);
                            }
                        });
                    }
                });
            }
            return convertView;
        }
    }

    class ViewHolder {
        TextView mTxtTitle;
        TextView mTxtValue;
    }
}
